<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>统计管理</el-breadcrumb-item>
      <el-breadcrumb-item>统计表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="" clearable v-model="uname" @clear="searchStatistical">
            <el-button slot="append" icon="el-icon-search" @click="searchStatistical"></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="addStatisticalFormVisible=true">添加迟到记录</el-button>
        </el-col>
      </el-row>

      <el-table :data="statisticalList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="员工编号" prop="eid" ></el-table-column>
        <el-table-column label="员工姓名" prop="uname" ></el-table-column>
        <el-table-column label="统计时间" prop="date" ></el-table-column>
        <el-table-column label="累计迟到次数" prop="count" ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 4 , 8, 16]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>

    <div>
      <!--    添加表单-->
      <el-dialog title="添加迟到记录" :visible.sync="addStatisticalFormVisible" :close-on-click-modal="false">
        <el-form ref="addChannelFormRef" :rules="addStatisticalFormRules" :model="addStatisticalForm" label-width="80px" size="mini">

          <el-form-item label="员工姓名" prop="uname">
            <el-input v-model="addStatisticalForm.uname"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
                v-model="addStatisticalForm.date"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="迟到次数" prop="count">
            <el-input v-model="addStatisticalForm.count"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelAddStatisticalDialog">取 消</el-button>
          <el-button type="primary" @click="addStatistical">确 定</el-button>
        </div>
      </el-dialog>

<!--      修改表单-->
      <el-dialog title="修改迟到记录" :visible.sync="modifyStatisticalFormVisible" :close-on-click-modal="false">
        <el-form ref="addChannelFormRef" :rules="modifyStatisticalFormRules" :model="modifyStatisticalForm" label-width="80px" size="mini">

          <el-form-item label="员工姓名" prop="uname">
            <el-input v-model="modifyStatisticalForm.uname" disabled></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
                v-model="modifyStatisticalForm.date"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="迟到次数" prop="count">
            <el-input v-model="modifyStatisticalForm.count"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelModifyStatisticalDialog">取 消</el-button>
          <el-button type="primary" @click="modifyStatistical">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>


</template>

<script>
export default {
  name: "Statistical",
  data(){
    return{
      uname:'',
      statisticalList:[],
      addStatisticalFormVisible:false,
      pageNum: 1,
      pageSize: 2,
      total: 0,

      // 添加表单
      addStatisticalForm:{
        eid:'',
        uname:'',
        date:'',
        count:'',
      },
      addStatisticalFormRules:{

      },

      //修改表单
      modifyStatisticalForm:{
        uname:'',
        date:'',
        count:'',
      },
      modifyStatisticalFormRules:{

      },
      modifyStatisticalFormVisible:false,

    }
  },
  methods:{
    handleSizeChange(size){
      this.pageSize = size;
      this.initStatistical();
    },
    handleCurrentChange(now){
      this.pageNum = now;
      this.initStatistical();
    },
    cancelAddStatisticalDialog(){
      this.addStatisticalFormVisible=false;
      this.addStatisticalForm={};
    },
    cancelModifyStatisticalDialog(){
      this.modifyStatisticalFormVisible=false;
    },
    searchStatistical(){
      this.initStatistical();
    },
    initStatistical(){
      let url = this.$HRMSUrl + "/statistical/statisticalList";
      console.log(this.uname)
      this.$axios.get(url, {
        params: {
          uname: this.uname,
          type: this.type,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.statisticalList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
            duration: 1000
          })
        }
      }).catch(error => {
        this.$message({
          type: "error",
          message: "系统错误，请联系管理员！",
          duration: 1000
        })
      })
    },
    showEditDialog(row){
      this.modifyStatisticalForm=row;
      this.modifyStatisticalFormVisible=true;
    },
    addStatistical(){
      let url = this.$HRMSUrl + "/statistical/add";
        console.log(this.addStatisticalForm)
      this.$axios.post(url,this.addStatisticalForm).then(
          res=>{
            if (res.data.code == 200){
              this.addStatisticalFormVisible = false;
              this.addStatisticalForm = {};
              this.initStatistical();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
    removeById(id){
      let url = this.$HRMSUrl+"/statistical/del/"+id;
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.initStatistical();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    modifyStatistical(){
      let url = this.$HRMSUrl+"/statistical/modify";
      this.$axios.put(url,this.modifyStatisticalForm)
          .then(res=>{
            if (res.data.code == 200){
              this.modifyStatisticalFormVisible = false ;
              this.initStatistical();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
  },
  created() {
    this.initStatistical();
  }

}
</script>

<style scoped>

</style>